package org.javaboy.vhr.service;

import org.javaboy.vhr.mapper.SalaryMapper;
import org.javaboy.vhr.model.Salary;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class SalaryService {
    private static final Logger logger = LoggerFactory.getLogger(SalaryService.class);
    
    @Autowired
    SalaryMapper salaryMapper;

    public List<Salary> getAllSalaries() {
        logger.info("獲取所有薪資配置");
        List<Salary> salaries = salaryMapper.getAllSalaries();
        logger.debug("獲取到 {} 條薪資配置記錄", salaries.size());
        return salaries;
    }

    public Salary getSalaryById(Integer id) {
        logger.info("根據ID獲取薪資配置，ID：{}", id);
        Salary salary = salaryMapper.selectByPrimaryKey(id);
        if (salary != null) {
            logger.debug("獲取到薪資配置：{}", salary);
        } else {
            logger.warn("未找到ID為 {} 的薪資配置", id);
        }
        return salary;
    }

    public Integer addSalary(Salary salary) {
        logger.info("添加薪資配置: {}", salary);
        salary.setCreateDate(new Date());
        Integer result = salaryMapper.insertSelective(salary);
        if (result > 0) {
            logger.info("薪資配置添加成功，ID: {}", salary.getId());
        } else {
            logger.error("薪資配置添加失敗");
        }
        return result;
    }

    public Integer deleteSalaryById(Integer id) {
        logger.info("刪除薪資配置，ID: {}", id);
        Integer result = salaryMapper.deleteByPrimaryKey(id);
        if (result > 0) {
            logger.info("薪資配置刪除成功");
        } else {
            logger.error("薪資配置刪除失敗");
        }
        return result;
    }

    public Integer updateSalaryById(Salary salary) {
        logger.info("更新薪資配置: {}", salary);
        Integer result = salaryMapper.updateByPrimaryKeySelective(salary);
        if (result > 0) {
            logger.info("薪資配置更新成功");
        } else {
            logger.error("薪資配置更新失敗");
        }
        return result;
    }
}
